 /*
  * Copyright (c) 2015 sb Jung.
  * All right reserved.
  */
package kr.co.j2b.common;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * <pre>
 * kr.co.j2b.common 
 *    |_ MyBatisAbstractSqlSession.java
 * </pre>
 * @date : 2015. 4. 7. 오후 3:10:28
 * @author : J-PC
 */
public class MyBatisAbstractSqlSession {
	
	Log logger = LogFactory.getLog(getClass());
	
	public SqlSession sqlSession;

	public SqlSession sqlSessionMysql01;
	public SqlSession sqlSessionMysql02;
	
	
	@Autowired
	@Resource(name="sqlSessionMysql01")
	public void setSqlSession03(SqlSession sqlSession) {
		this.sqlSessionMysql01 = sqlSession;
	}
	
	@Autowired
	@Resource(name="sqlSessionMysql02")
	public void setSqlSession04(SqlSession sqlSession) {
		this.sqlSessionMysql02 = sqlSession;
	}
	
	DataSourceTransactionManager transactionManager;
	
	@Autowired
	@Qualifier("transactionManagerMysql01")
	private DataSourceTransactionManager transactionManagerMysql01;
	
	@Autowired
	@Qualifier("transactionManagerMysql02")
	private DataSourceTransactionManager transactionManagerMysql02;	
	
	
	public SqlSession getSqlSession(String ds){
		if( "sqlSessionMysql01".equals(ds) ){
			sqlSession = sqlSessionMysql01;
		}else if( "sqlSessionMysql02".equals(ds) ){
			sqlSession = sqlSessionMysql02;
		}else{
			sqlSession = sqlSessionMysql01;
		}
		
		return sqlSession;
	}
	
	public DataSourceTransactionManager getTransaction(String ds){
		if( "sqlSessionMysql01".equals(ds) ){
			transactionManager = transactionManagerMysql01;
		}else if( "sqlSessionMysql02".equals(ds) ){
			transactionManager = transactionManagerMysql02;
		}else{
			transactionManager = transactionManagerMysql01;
		}
		
		return transactionManager;
	}
}
